﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.ScrollLayout;
using Spinning.Spinning.Scenes.ProcessScene.Items;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework.Input.Touch;

namespace Spinning.Spinning.Scenes.ProcessScene.Store
{
    class ScrollLayoutElementItemForStore : ScrollLayoutElement
    {
        Item item;
        ItemDescription itemDes;
        TextEntity _priceText, _quantityText, _buyQuantityText;
        Sprite _starSprite, _pinkBackground, _buyQuantityBG;
        public ArrowButtonForStore arrR,arrL;
        int _quantity, _buyQuantity,_spinningPrice;

        public int Quantity 
        {
            get { return _quantity; }
            set
            {
                _quantity = value;
                if (_quantityText != null)
                {
                    _quantityText.Value = _quantity.ToString();
                }
            }
        }
        public int BuyQuantity
        {
            get { return _buyQuantity; }
            set
            {
                _buyQuantity = value;
                if (_buyQuantityText != null)
                {
                    _buyQuantityText.Value = _buyQuantity.ToString();
                }
            }
        }

        public int SpinningPrice
        {
            get
            {
                return _spinningPrice;
            }
            set
            {
                _spinningPrice = value;
                if (_priceText != null)
                    _priceText.Value = "" + _spinningPrice;
            }
        }

        public enum StateType
        {
            Focus, Normal
        }

        public StateType State_Type
        {
            get;
            set;
        }

        BaseScene _scene;

        public ScrollLayoutElementItemForStore(BaseScene scene)
        {
            Height = 125;
            Origin = new Vector2(.5f);
            ItemType = Constants.ItemTypes.Marble;
            State_Type = StateType.Normal;
            BuyQuantity = 0;
            SpinningPrice = 200;
            _scene = scene;
        }

        public override void OnLoad()
        {
            base.OnLoad();

            _pinkBackground = new Sprite();
            _pinkBackground.textureName = "item_store/pink_bg";
            _pinkBackground.Origin = new Vector2(.5f);
            _pinkBackground.OnLoad();
            Attach(_pinkBackground);

            _buyQuantityBG = new Sprite();
            _buyQuantityBG.textureName = "item_store/rectangle";
            _buyQuantityBG.Origin = new Vector2(.5f);
            _buyQuantityBG.OnLoad();
            Attach(_buyQuantityBG);

            arrR = new ArrowButtonForStore();
            arrR.OnLoad();
            arrR.OnTouchEvent += OnRightTouch;
            arrR.animationSprite.DrawEffect = Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipHorizontally;
            Attach(arrR);
            _scene.TouchManager.RegisteTouchArea(arrR);

            arrL = new ArrowButtonForStore();
            arrL.OnLoad();
            arrL.OnTouchEvent += OnLeftTouch;
            Attach(arrL);
            _scene.TouchManager.RegisteTouchArea(arrL);

            item = new Item();
            item.OnLoad();
            Attach(item);
            itemDes = new ItemDescription();
            itemDes.OnLoad();
            Attach(itemDes);

            _starSprite = new Sprite();
            _starSprite.textureName = "item_store/star_s";
            _starSprite.Origin = new Vector2(.5f);
            _starSprite.ZIndex = Constants.ui;
            _starSprite.OnLoad();
            Attach(_starSprite);

            _priceText = new TextEntity();
            ResourceManager.ContentRoot = "";
            _priceText.fontString = "Century Gothic";
            _priceText.OnLoad();
            _priceText.Origin = new Vector2(0, 0.5f);
            _priceText.Scale = new Vector2(1.2f);
            _priceText.Color = Color.Red;
            _priceText.Static = false;
            _priceText.ZIndex = Constants.ui;
            Attach(_priceText);
            _priceText.Value = SpinningPrice.ToString();

            _quantityText = new TextEntity();
            _quantityText.fontString = "CenturyGothicBold";
            _quantityText.OnLoad();
            _quantityText.Origin = new Vector2(0, 0.5f);
            _quantityText.Color = new Color(54, 56, 43);//咖啡
            _quantityText.Static = false;
            _quantityText.ZIndex = Constants.ui;
            Attach(_quantityText);
            _quantityText.Value = Quantity.ToString();

            _buyQuantityText = new TextEntity();
            _buyQuantityText.fontString = "CenturyGothicBold";
            _buyQuantityText.OnLoad();
            _buyQuantityText.Origin = new Vector2(0.5f);
            _buyQuantityText.Color = Color.White;
            _buyQuantityText.Static = false;
            _buyQuantityText.ZIndex = Constants.ui;
            Attach(_buyQuantityText);
            _buyQuantityText.Value = BuyQuantity.ToString();

            item.Position = pos - new Vector2(105, 0);
            itemDes.Position = pos + new Vector2(65, 20);
            _starSprite.Position = pos + new Vector2(-25, -35);
            _priceText.Position = pos + new Vector2(0, -33);
            _quantityText.Position = pos + new Vector2(-155, 45);
            _buyQuantityText.Position = pos + new Vector2(114, -33);
            _pinkBackground.Position = pos + new Vector2(0, -5);
            _buyQuantityBG.Position = pos + new Vector2(114, -33);
            arrR.Position = pos + new Vector2(135, -52);
            arrL.Position = pos + new Vector2(65, -52); 
        }

        Vector2 pos = new Vector2(0, 125 / 2);

        void OnLeftTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            BuyQuantity--;
        }

        void OnRightTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            BuyQuantity++;
        }

        public Constants.ItemTypes ItemType
        {
            get;
            set;
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            item.ItemType = itemDes.ItemType = ItemType;
            switch (State_Type)
            {
                case StateType.Focus:
                    itemDes.Color = new Color(164, 0, 0);//紅
                    _priceText.Color = new Color(164, 0, 0);//紅
                    _buyQuantityBG.Color = new Color(164, 0, 0);//紅
                    _pinkBackground.Alpha = 1;
                    arrR.isEnable = true;
                    arrL.isEnable = true;
                    if (BuyQuantity == 0) arrL.isEnable = false;
                    else if (BuyQuantity == 99) arrR.isEnable = false;
                    break;
                case StateType.Normal:
                    itemDes.Color = new Color(54, 56, 43);//咖啡
                    _priceText.Color = new Color(54, 56, 43);//咖啡
                    _buyQuantityBG.Color = new Color(54, 56, 43);//咖啡
                    _pinkBackground.Alpha = 0;
                    arrR.isEnable = false;
                    arrL.isEnable = false;
                    break;
            }
        }

        public override bool Contain(Vector2 _position)
        {
            return isTouch(_pinkBackground, _position);
        }

        bool isTouch(Sprite _checkBox, Vector2 _position)
        {
            Vector2 position = Vector2.Transform(_position, _checkBox.UnProjection);
            Vector2 topLeft = _checkBox.Position - _checkBox.Origin * new Vector2(_checkBox.SpriteSize.X, _checkBox.SpriteSize.Y);
            Vector2 rightDown = topLeft + new Vector2(_checkBox.SpriteSize.X, _checkBox.SpriteSize.Y);
            bool h = (position.X > topLeft.X && position.X < rightDown.X && position.Y > topLeft.Y && position.Y < rightDown.Y);
            if(!h)
                State_Type = StateType.Normal;
            return h;
        }

        public override void OnTouch(Microsoft.Xna.Framework.Input.Touch.TouchLocation touchLocation)
        {
            if (State_Type == StateType.Normal)
                State_Type = StateType.Focus;
        }

    }
}
